using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.CCT
{
    public class Label : IComponent
    {

        protected Rectangle labelRectangle = new Rectangle(0, 0, 100, 25);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush labelBrush = new SolidBrush(Color.Empty) ;
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected string text;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name;


        #region Properties
        public Point Location
        {
            get { return labelRectangle.Location; }
            set 
            { 
                labelRectangle.Location = value;
                Invalidate(); 
            }
        }

        public Rectangle Bounds
        {
            get { return labelRectangle; }
            set 
            { 
                labelRectangle = value; 
                Invalidate(); 
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set 
            { 
                font = value; 
                Invalidate();
            }
        }

        public string Text
        {
            get { return text; }
            set 
            { 
                text = value; 
                Invalidate();
            }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return labelBrush.Color; }
            set 
            { 
                labelBrush.Color = value; 
                Invalidate(); 
            }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion


        public Label()
        {
            labelFormat.LineAlignment = StringAlignment.Center;
            labelFormat.Alignment = StringAlignment.Center;
            text = "I'm a Label!";
        }

        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }


        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                g.FillRectangle(labelBrush, labelRectangle);
                g.DrawString(text, font, Brushes.Black, (RectangleF)labelRectangle, labelFormat);

                //Draw a box around the button to show that it has focus
                if (hasFocus)
                {
                    g.DrawRectangle(Pens.LightBlue, labelRectangle.Left, labelRectangle.Top, labelRectangle.Width, labelRectangle.Height);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            Invalidate();
        }


        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (e.KeyChar == '\r')
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }

            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }



        #region IComponent Members


        public event EventHandler KeyDown;

        public void OnKeyDown(KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }

        #endregion
    }
}
